package com.nova.auth.controller.wx;

import cn.hutool.extra.qrcode.QrCodeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

import static com.nova.auth.constant.Constants.REDIRECT_URI;

/**
 * @author ttao
 * @since 2024/10/19 16:04
 */
@RestController
public class WxChatController {

    @GetMapping("/wx/doLogin")
    public void wxLogin(HttpServletResponse response) throws Exception {

        // redirect_uri是回调地址，注意要转成UrlEncode格式
        String redirectUrl = URLEncoder.encode(REDIRECT_URI, "UTF-8");

        // 构造二维码链接地址
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxed5e359a60b1025e&redirect_uri="
                + redirectUrl + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

        // 生成二维码，扫描后跳转上面的地址
        response.setContentType("image/png");
        QrCodeUtil.generate(url, 300, 300, "jpg", response.getOutputStream());

    }
}
